from pyhanlp import *


def Fenci(text):
    ans=[]
    all_sentences=text.split('\n')
    for sentence in all_sentences:
        elems=HanLP.segment(sentence)
        for elem in elems:
            elem=str(elem)
            lis=elem.split('/')
            if lis[1]=='w':
                continue
            flag=0
            for i in range(0,len(ans)):
                if lis[0] == ans[i]['word']:
                    ans[i]['num']+=1
                    flag=1
                    break
                # elif lis[0] == ans[i]['word'] and (i+1==len(ans) or lis[0] == ans[i+1]['word']):
                #     ans.insert(i+1,{'word':lis[0],'pos':lis[1],'num':1})
                #     flag=1
            
            if flag==0:
                ans.append({'word':lis[0],'num':1})
    return ans
            


    # with open ('word1.txt','w',encoding='utf-8') as fp:
    #     fp.write(i)
    # print(i)
# with open ('./cheng.txt','r',encoding='utf-8') as fh:
#     all_sentence=fh.read()
#     all_sentence=all_sentence.split('\n')

# with open ('cheng_word.txt','w',encoding='utf-8') as fp:
#     fp.write('word  pos num\n')
#     for i in ans:
#         for j in i.values():
#             fp.write(str(j)+'   ')
#         fp.write('\n')
# a=Fenci("中国人喜欢吃饭")
# print(a)